//
//  HEHealthPlaneDetailsController.m
//  HealthFamily
//
//  Created by 吴欧 on 2020/12/15.
//  Copyright © 2020 honglutec. All rights reserved.
//

#import "HEHealthPlaneDetailsController.h"
#import "HEHealthPlaneModel.h"
#import "HEDieaseItemCell.h"

@interface HEHealthPlaneDetailsController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) GDTableView *tableView;
@property (nonatomic, strong) NSMutableArray *sections;
@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic, assign) BOOL isHasPrivate;
@property (nonatomic, strong) HEDoctorInfoModel *docInfo;
@property (nonatomic, assign) CGFloat topHeight;
@property (nonatomic, strong) GDFormModel *formModel;
@property (nonatomic, strong) HEHealthPlaneModel *details;

@end

@implementation HEHealthPlaneDetailsController

- (GDFormModel *)formModel {
    if (!_formModel) {
        _formModel = [[GDFormModel alloc] init];
        _formModel.proxyVC = self;
    }
    return _formModel;
}
 
- (void)viewDidLoad {
   [super viewDidLoad];
   // Do any additional setup after loading the view.
    self.title = @"健康规划单";
     
    [self loadData];
}

- (void)initUI {
    [self initNoticeTopView];

    [self.view addSubview:self.tableView];
    
    [self initCell];
}

- (GDTableView *)tableView {
   if (!_tableView) {
       _tableView = [[GDTableView alloc] initWithFrame:CGRectMake(0, ZXNavBarHeight + _topHeight, KScreenWidth, KScreenHeight - ZXNavBarHeight - _topHeight) style:UITableViewStylePlain];
//        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
       _tableView.separatorColor = CSplitLineColor;
       _tableView.scrollsToTop = YES;
       _tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 0.01)];
       _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 0.01)];

       _tableView.showsVerticalScrollIndicator = NO;
       _tableView.backgroundColor = CViewBgColor;
       _tableView.delegate = self;
       _tableView.dataSource = self;
       
   }
   return _tableView;
}

- (void)initNoticeTopView {
    
    NSString *nstr = [NSString stringWithFormat:@"规划医师：%@    %@", _details.doc_name, [NSDate timestampToDate:_details.create_date formaterMode: NSDateFormaterModeDay]];
    _topHeight = [nstr heightForFont:DYFont(16) width:KScreenWidth-30] + 30;
    UIView *noticeWarp = [[UIView alloc] initWithFrame: CGRectMake(0, ZXNavBarHeight, KScreenWidth, _topHeight )];
    noticeWarp.backgroundColor = KHexColor(@"E8F2FE");
    [self.view addSubview:noticeWarp];
 
    UILabel *noticeLb = [UILabel new];
    noticeLb.text = nstr;
    noticeLb.textColor = CNavBgColor;
    noticeLb.numberOfLines = 0;
    noticeLb.font = DYFont(16);
    [noticeWarp addSubview:noticeLb];
    
    [noticeLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(noticeWarp).mas_offset(10);
        make.left.equalTo(noticeWarp).mas_offset(15);
        make.right.equalTo(noticeWarp).mas_offset(-15);
        make.bottom.equalTo(noticeWarp).mas_offset(-10);
    }];
}

- (void)loadData {
    [WOAFNetWorkingHandle POSTWithBaseUrl:KServerUrl suffixUrl:KUserPlaneDetails parameters:@{@"inst_id":[NSNumber numberWithInteger:_inst_id]} success:^(id  _Nonnull responseObject) {
        HEResponse *response = [HEResponse mj_objectWithKeyValues:responseObject];
        self.details = [HEHealthPlaneModel mj_objectWithKeyValues: response.data];
        [self initUI];
    } failure:^(NSError * _Nonnull error) {
        
    }];
}

- (void)initCell {
   
   _sections = @[].mutableCopy;

    NSMutableArray *classArr = [NSMutableArray array];
   
    NSArray *titles = @[@"营养", @"运动", @"心理", @"生活习惯", @"药物", @"指导意见"];
   
    GDFiterCellModel *consulModel = [GDFiterCellModel new];
    consulModel.cellKlass = HEDieaseItemCell.class;
     

   // 添加的时候注意顺序，暂未对顺序做处理
   classArr = @[consulModel].mutableCopy;
   [self.tableView mc_registerCellByFiterModels:classArr];
   
    for (NSString *str in titles) {
        GDFiterCellModel *model = [GDFiterCellModel new];
        model.cellKlass = HEDieaseItemCell.class;
        model.leftTitle = str;
        model.leftTitleColor = CNavBgColor;
        model.contentColor = CNavLightBlackColor;
        
        [_sections addObject:model];
    }
      
    self.formModel.ai_content = @[IsNotNull(_details.nutrition), IsNotNull(_details.sport), IsNotNull(_details.psychology)
                          , IsNotNull(_details.bad_habits), IsNotNull(_details.drugs) , IsNotNull(_details.guidance)];
  
   [self.tableView reloadData];
}
 

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _sections.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    GDFiterCellModel *fiterModel = _sections[indexPath.row];
    UITableViewCell *cell = [_tableView mc_dequeueReusableCellWithClassType:fiterModel.cellKlass];
    fiterModel.fiterCellModelBlock((MCTableBaseCell *)cell, fiterModel, self.formModel, indexPath);
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    GDFiterCellModel *fiterModel = _sections[indexPath.row];
    return [tableView fd_heightForCellWithIdentifier: NSStringFromClass(fiterModel.cellKlass) cacheByIndexPath:indexPath configuration:^(MCTableBaseCell *cell) {
        [cell refreshContent:fiterModel formModel: self.formModel indexPath:indexPath];
    }];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

}


@end
